<?php

namespace app\api\controller;

use app\api\validation\Subject;
use app\services\SubjectService;
use plugin\admin\app\model\SubjectCategory;
use support\Db;
use support\Request;

class SubjectController
{
    public function create(Request $request)
    {
        try {
            $validate = new Subject();
            $data = $request->post();
            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            $isEnable = SubjectService::create($data);

            $msg = '成功';
            if (!$isEnable) {
                $msg = '审核中，请稍候';
            }

            return ok_rsp($msg);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function categories(Request $request)
    {
        try {
            $categories = SubjectService::categories();

            return ok_rsp('成功', ['categories' => $categories]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function list(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $id = $request->post('id', 0);
            $list = SubjectService::list($page, 10, $id);

            return ok_rsp('成功', $list);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function detail(Request $request)
    {
        try {
            $id = $request->post('id', 0);
            $data = SubjectService::detail($id);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function like(Request $request)
    {
        try {
            $id = $request->post('id', 0);
            SubjectService::like($id);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function unlike(Request $request)
    {
        try {
            $id = $request->post('id', 0);
            SubjectService::unlike($id);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function comment(Request $request)
    {
        try {
            $id = $request->post('id', 0);
            $content = $request->post('content', 0);
            SubjectService::comment($id, $content);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function reply(Request $request)
    {
        try {
            $id = $request->post('id', 0);
            $content = $request->post('content', 0);
            SubjectService::reply($id, $content);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function commentList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $id = $request->post('id', 0);
            $data = SubjectService::commentList($page, 10, $id);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function surnameList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $data = SubjectService::surnameList($page, 10);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function likeUserList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $id = $request->post('id');

            $data = SubjectService::likeUserList($page, 10, $id);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}